package com.cash.lemoloan.jsbridge.Utils;

import android.os.Bundle;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Set;

public class JsonUtil {
    public static  org.json.JSONObject bundleToJSON(Bundle bundle){
        org.json.JSONObject json = new org.json.JSONObject();
        try {


            if (bundle == null || bundle.isEmpty()) {
                return json;
            }
            Set<String> keySet = bundle.keySet();
            for (String key : keySet) {
                Object object = bundle.get(key);
                if (object instanceof String || object instanceof Boolean || object instanceof Integer
                        || object instanceof Long || object instanceof Double) {
                    json.put(key, object);
                }
            }        }catch (JSONException e){
            return null;
        }

        return json;
    }

    public static JSONArray loadJsonArray(String jsonArrayString) {
        try {
            if (jsonArrayString != null && jsonArrayString.length() != 0) {
                return new JSONArray(jsonArrayString);
            }
        } catch (Throwable e) {
        }
        return null;
    }
    public static JSONObject loadJSON(String jsonString) {
        try {
            if (jsonString != null && jsonString.length() != 0) {
                return new JSONObject(jsonString);
            }
        } catch (Throwable e) {
        }
        return null;
    }
    public static JSONObject putObject(JSONObject aJoObj, String aName, Object aValue) {
        try {
            if (aJoObj == null) {
                aJoObj = new JSONObject();
            }
            aJoObj.put(aName,aValue);
        } catch (Throwable e) {

        }
        return aJoObj;
    }
}
